# encoding='utf-8'
"""
@Author: 张晟烨
@Date: 2022/11/25
@Email: zhangsy@zylliondata.com
@Description: 请求
@source：
@Version: Python3.8
@Modified By: 

"""
import requests


def request_text(url, headers=None):
    headers = headers if headers else {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;"
                  "q=0.8,application/signed-exchange;v=b3",
        "Accept-Language": "zh,en-US;q=0.9,en;q=0.8,zh-CN;q=0.7,und;q=0.6",
    }

    HTTPS_PROXY_API = "https://ip-pool.data4industry.com/"
    pro = requests.get(HTTPS_PROXY_API,verify=False).text
    print(pro)
    d = {'https': f'http://{pro}'}
    d1 = {'http': f'http://{pro}'}
    try:
        response = requests.get(url, headers=headers, proxies=d1, timeout=20)
        response.encoding = response.apparent_encoding
    except:
        try:
            response = requests.get(url, headers=headers, proxies=d, timeout=20)
            response.encoding = response.apparent_encoding
        except:
            raise SyntaxError('爬取网站超时')

    if response.status_code == 200:
        return response.text
    else:
        raise ValueError(f'状态码异常{response.status_code}')


if __name__ == '__main__':
    print(request_text('https://www.electrive.com/category/automobile/page/2/'))
